﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InfiniteRogue.GameContent
{
    public class RandomMonster
    {
        private static RandomMonster instace;
        public static RandomMonster Instance
        {
            get { return instace ?? (instace = new RandomMonster()); }
        }

        private RandomMonster()
        {
            races = new Dictionary<string, StatsTemplate>();
            classes = new Dictionary<string, StatsTemplate>();
            classes.Add("rogue", DefaultClass());
            races.Add("human", DefaultRace());
            
            StatsTemplate dwarf = new StatsTemplate();
            dwarf.StatAlotment[StatSystem.STR] = .5f;
            dwarf.StatAlotment[StatSystem.END] = .5f;
            races.Add("dwarf", dwarf);

            StatsTemplate shade = new StatsTemplate();
            shade.StatAlotment[StatSystem.INT] = 1f;
            races.Add("shade", shade);

            StatsTemplate gladiator = new StatsTemplate();
            gladiator.StatAlotment[StatSystem.ATTACK] = .2f;
            gladiator.StatAlotment[StatSystem.DODGE] = .2f;
            gladiator.StatAlotment[StatSystem.CRITICAL] = .2f;
            gladiator.StatAlotment[StatSystem.STUN] = .2f;
            gladiator.StatAlotment[StatSystem.PARRY] = .2f;
            classes.Add("gladiator", gladiator);
        }

        public Character GetRandomMonster(string className, string raceName, int level)
        {
            if(string.IsNullOrWhiteSpace(raceName)) raceName = "monstrous";
            if(string.IsNullOrWhiteSpace(className)) className = "beast";

            Character mob = new Character();
            mob.Tile.Character = raceName[0];
            mob.Tile.Color = System.Drawing.Color.Red;
            mob.Name = raceName + " " + className;

            StatsTemplate monsterRace = races.ContainsKey(raceName.ToLower()) ? races[raceName.ToLower()] : DefaultRace();
            StatsTemplate monsterClass = classes.ContainsKey(className.ToLower()) ? classes[className.ToLower()] : DefaultClass();
            
            int statPoints = level * 5;
            int skillPoints = level * 5;

            foreach (var alotment in monsterClass.StatAlotment)
            {
                mob.Stats[alotment.Key] = (int)Math.Round(skillPoints * alotment.Value);
            }

            foreach (var alotment in monsterRace.StatAlotment)
            {
                mob.Stats[alotment.Key] = 5 + (int)Math.Round(statPoints * alotment.Value);
            }

            return mob;
        }

        public Character GetRandomMonster(int level)
        {
            return GetRandomMonster(classes.Keys.Random(), races.Keys.Random(), level);            
        }

        public StatsTemplate DefaultClass()
        {
            StatsTemplate defaultClass = new StatsTemplate();
            defaultClass.StatAlotment[StatSystem.ATTACK] = .5f;
            defaultClass.StatAlotment[StatSystem.DODGE] = .5f;
            return defaultClass;
        }

        public StatsTemplate DefaultRace()
        {
            StatsTemplate defaultRace = new StatsTemplate();
            defaultRace.StatAlotment[StatSystem.STR] = .2f;
            defaultRace.StatAlotment[StatSystem.END] = .2f;
            defaultRace.StatAlotment[StatSystem.AGI] = .2f;
            defaultRace.StatAlotment[StatSystem.WIS] = .2f;
            defaultRace.StatAlotment[StatSystem.INT] = .2f;
            return defaultRace;
        }

        private Dictionary<string, StatsTemplate> races;
        private Dictionary<string, StatsTemplate> classes;
    }
}
